using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Zhaoxi.MSACommerce.HttpApi.Common.Infrastructure;
using ZhaoXi.MSACommerce.UserCases.Common.Interfaces;
using ZhaoXi.MSACommerce.UserService.HttpApi.Servcie;
using ZhaoXi.MSACommerce.UserService.UserCases.Commands;
using ZhaoXi.MSACommerce.UserService.UserCases.Queries;

namespace ZhaoXi.MSACommerce.UserService.HttpApi.Controllers;

[Route("api/user")]
[ApiController]
public class UserController(IVerificationService verificationService):ApiControllerBase
{
    [HttpGet]
    public async Task<IActionResult> Get([FromQuery]GetUserQuery request)
    {
        var result = await Sender.Send(request);
        
        return ReturnResult(result);
    }

    [HttpPost]
    public async Task<IActionResult> Create(CreateUserDto request)
    {
        var response=await verificationService.GetVerificationAsync(request.Phone,request.Code);
        if (!response. IsSuccessStatusCode) return BadRequest(response. Error. Content);
        
        var result = await Sender.Send(new CreateUserCommand(request.Username,request.Password,request.Phone));

        return ReturnResult(result);
    }

    [HttpGet(template: "test")]
    [Authorize]
    public IActionResult Get([FromServices] IUser user)
    {
        return Ok(user);
    }
    
}

public record CreateUserDto(string Username, string Password, string Phone, string Code);